import 'package:flutter/material.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import 'package:animations/animations.dart';
import 'package:animate_do/animate_do.dart';
import '../animations/animation_config.dart';

/// 高级加载动画组件
/// 封装loading_animation_widget，提供丰富的加载动画效果
class AdvancedLoadingAnimations {
  
  /// 脉动圆点动画
  static Widget pulseDots({
    double size = 50,
    Color? color,
  }) {
    return LoadingAnimationWidget.threeArchedCircle(
      color: color ?? const Color(0xFF1976D2), // 使用默认蓝色系主色调
      size: size,
    );
  }

  /// 旋转圆圈动画
  static Widget rotatingCircle({
    double size = 50,
    Color? color,
  }) {
    return LoadingAnimationWidget.threeRotatingDots(
      color: color ?? const Color(0xFF1976D2), // 使用默认蓝色系主色调
      size: size,
    );
  }

  /// 波浪动画
  static Widget wave({
    double size = 50,
    Color? color,
  }) {
    return LoadingAnimationWidget.waveDots(
      color: color ?? const Color(0xFF1976D2), // 使用默认蓝色系主色调
      size: size,
    );
  }

  /// 闪烁网格动画
  static Widget flickrGrid({
    double size = 50,
    Color? color,
  }) {
    return LoadingAnimationWidget.flickr(
      leftDotColor: color ?? const Color(0xFF1976D2),
      rightDotColor: color?.withOpacity(0.6) ?? const Color(0xFF1976D2).withOpacity(0.6),
      size: size,
    );
  }

  /// 弹跳球动画
  static Widget bouncingBall({
    double size = 50,
    Color? color,
  }) {
    return LoadingAnimationWidget.bouncingBall(
      color: color ?? const Color(0xFF1976D2), // 使用默认蓝色系主色调
      size: size,
    );
  }

  /// 拍打动画
  static Widget beat({
    double size = 50,
    Color? color,
  }) {
    return LoadingAnimationWidget.beat(
      color: color ?? const Color(0xFF1976D2), // 使用默认蓝色系主色调
      size: size,
    );
  }

  /// 新月动画
  static Widget newtonCradle({
    double size = 50,
    Color? color,
  }) {
    return LoadingAnimationWidget.newtonCradle(
      color: color ?? const Color(0xFF1976D2), // 使用默认蓝色系主色调
      size: size,
    );
  }

  /// 进度条动画
  static Widget progressiveDots({
    double size = 50,
    Color? color,
  }) {
    return LoadingAnimationWidget.progressiveDots(
      color: color ?? const Color(0xFF1976D2), // 使用默认蓝色系主色调
      size: size,
    );
  }

  /// 六角形动画
  static Widget hexagon({
    double size = 50,
    Color? color,
  }) {
    return LoadingAnimationWidget.hexagonDots(
      color: color ?? const Color(0xFF1976D2), // 使用默认蓝色系主色调
      size: size,
    );
  }

  /// 旋转正方形动画
  static Widget rotatingSquare({
    double size = 50,
    Color? color,
  }) {
    return LoadingAnimationWidget.fourRotatingDots(
      color: color ?? const Color(0xFF1976D2), // 使用默认蓝色系主色调
      size: size,
    );
  }
}

/// 页面过渡动画组件
/// 封装animations包，提供Material 3风格的页面转换
class PageTransitionAnimations {
  
  /// 淡入淡出过渡
  static Widget fadeThrough({
    required Widget child,
    Duration duration = AnimationConfig.medium,
    bool fillColor = false,
    Color? color,
  }) {
    return PageTransitionSwitcher(
      duration: duration,
      transitionBuilder: (child, primaryAnimation, secondaryAnimation) {
        return FadeThroughTransition(
          animation: primaryAnimation,
          secondaryAnimation: secondaryAnimation,
          fillColor: fillColor ? (color ?? Colors.transparent) : null,
          child: child,
        );
      },
      child: child,
    );
  }

  /// 共享轴过渡
  static Widget sharedAxis({
    required Widget child,
    SharedAxisTransitionType transitionType = SharedAxisTransitionType.horizontal,
    Duration duration = AnimationConfig.medium,
    bool fillColor = false,
    Color? color,
  }) {
    return PageTransitionSwitcher(
      duration: duration,
      transitionBuilder: (child, primaryAnimation, secondaryAnimation) {
        return SharedAxisTransition(
          animation: primaryAnimation,
          secondaryAnimation: secondaryAnimation,
          transitionType: transitionType,
          fillColor: fillColor ? (color ?? Colors.transparent) : null,
          child: child,
        );
      },
      child: child,
    );
  }

  /// 容器变换过渡
  static OpenContainer containerTransform({
    required Widget closedBuilder(BuildContext context, VoidCallback openContainer),
    required Widget openBuilder(BuildContext context, VoidCallback closeContainer),
    Duration transitionDuration = AnimationConfig.medium,
    Color? closedColor,
    Color? openColor,
    double closedElevation = 1.0,
    double openElevation = 4.0,
    ShapeBorder? closedShape,
    ShapeBorder? openShape,
    bool useRootNavigator = false,
    RouteSettings? routeSettings,
  }) {
    return OpenContainer(
      closedBuilder: closedBuilder,
      openBuilder: openBuilder,
      transitionDuration: transitionDuration,
      closedColor: closedColor ?? const Color(0xFFFFFFFF),
      openColor: openColor ?? const Color(0xFFFFFFFF),
      closedElevation: closedElevation,
      openElevation: openElevation,
      closedShape: closedShape ?? const RoundedRectangleBorder(),
      openShape: openShape ?? const RoundedRectangleBorder(),
      useRootNavigator: useRootNavigator,
      routeSettings: routeSettings,
      transitionType: ContainerTransitionType.fade,
    );
  }
}

/// 入场动画组件
/// 封装animate_do，提供丰富的入场动画效果
class EntranceAnimations {
  
  /// 淡入动画
  static Widget fadeIn({
    required Widget child,
    Duration duration = AnimationConfig.medium,
    Duration delay = Duration.zero,
    double from = 0.0,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return FadeIn(
      duration: duration,
      delay: delay,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }

  /// 淡出动画
  static Widget fadeOut({
    required Widget child,
    Duration duration = AnimationConfig.medium,
    Duration delay = Duration.zero,
    double from = 1.0,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return FadeOut(
      duration: duration,
      delay: delay,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }

  /// 从左滑入
  static Widget slideInLeft({
    required Widget child,
    Duration duration = AnimationConfig.medium,
    Duration delay = Duration.zero,
    double from = 100,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return SlideInLeft(
      duration: duration,
      delay: delay,
      from: from,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }

  /// 从右滑入
  static Widget slideInRight({
    required Widget child,
    Duration duration = AnimationConfig.medium,
    Duration delay = Duration.zero,
    double from = 100,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return SlideInRight(
      duration: duration,
      delay: delay,
      from: from,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }

  /// 从上滑入
  static Widget slideInUp({
    required Widget child,
    Duration duration = AnimationConfig.medium,
    Duration delay = Duration.zero,
    double from = 100,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return SlideInUp(
      duration: duration,
      delay: delay,
      from: from,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }

  /// 从下滑入
  static Widget slideInDown({
    required Widget child,
    Duration duration = AnimationConfig.medium,
    Duration delay = Duration.zero,
    double from = 100,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return SlideInDown(
      duration: duration,
      delay: delay,
      from: from,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }

  /// 缩放入场
  static Widget zoomIn({
    required Widget child,
    Duration duration = AnimationConfig.medium,
    Duration delay = Duration.zero,
    double from = 0.0,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return ZoomIn(
      duration: duration,
      delay: delay,
      from: from,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }

  /// 弹跳入场
  static Widget bounceIn({
    required Widget child,
    Duration duration = AnimationConfig.medium,
    Duration delay = Duration.zero,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return BounceIn(
      duration: duration,
      delay: delay,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }

  /// 弹性入场
  static Widget elasticIn({
    required Widget child,
    Duration duration = AnimationConfig.slow,
    Duration delay = Duration.zero,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return ElasticIn(
      duration: duration,
      delay: delay,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }

  /// 旋转入场
  static Widget rotateIn({
    required Widget child,
    Duration duration = AnimationConfig.medium,
    Duration delay = Duration.zero,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return Spin(
      duration: duration,
      delay: delay,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }

  /// 翻转入场
  static Widget flipInX({
    required Widget child,
    Duration duration = AnimationConfig.medium,
    Duration delay = Duration.zero,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return FlipInX(
      duration: duration,
      delay: delay,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }

  /// Y轴翻转入场
  static Widget flipInY({
    required Widget child,
    Duration duration = AnimationConfig.medium,
    Duration delay = Duration.zero,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return FlipInY(
      duration: duration,
      delay: delay,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }

  /// 摇摆动画
  static Widget swing({
    required Widget child,
    Duration duration = AnimationConfig.slow,
    Duration delay = Duration.zero,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return Swing(
      duration: duration,
      delay: delay,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }

  /// 跳动动画
  static Widget bounce({
    required Widget child,
    Duration duration = AnimationConfig.slow,
    Duration delay = Duration.zero,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return Bounce(
      duration: duration,
      delay: delay,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }

  /// 闪烁动画
  static Widget flash({
    required Widget child,
    Duration duration = AnimationConfig.slow,
    Duration delay = Duration.zero,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return Flash(
      duration: duration,
      delay: delay,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }

  /// 脉冲动画
  static Widget pulse({
    required Widget child,
    Duration duration = AnimationConfig.slow,
    Duration delay = Duration.zero,
    bool animate = true,
    bool infinite = false,
    VoidCallback? onFinish,
  }) {
    return Pulse(
      duration: duration,
      delay: delay,
      animate: animate,
      child: child,
      onFinish: (direction) => onFinish?.call(),
    );
  }
}

/// 列表项动画组件
/// 为列表项提供统一的入场动画
class ListItemAnimations {
  
  /// 创建列表项动画序列
  static List<Widget> createStaggeredList({
    required List<Widget> children,
    Duration duration = AnimationConfig.listItemEnter,
    Duration staggerDelay = const Duration(milliseconds: 50),
    AnimationType type = AnimationType.slideInUp,
  }) {
    return children.asMap().entries.map((entry) {
      final index = entry.key;
      final child = entry.value;
      final delay = Duration(milliseconds: staggerDelay.inMilliseconds * index);
      
      switch (type) {
        case AnimationType.slideInUp:
          return EntranceAnimations.slideInUp(
            child: child,
            duration: duration,
            delay: delay,
          );
        case AnimationType.slideInLeft:
          return EntranceAnimations.slideInLeft(
            child: child,
            duration: duration,
            delay: delay,
          );
        case AnimationType.fadeIn:
          return EntranceAnimations.fadeIn(
            child: child,
            duration: duration,
            delay: delay,
          );
        case AnimationType.zoomIn:
          return EntranceAnimations.zoomIn(
            child: child,
            duration: duration,
            delay: delay,
          );
      }
    }).toList();
  }

  /// 为单个列表项添加动画
  static Widget animateListItem({
    required Widget child,
    required int index,
    Duration duration = AnimationConfig.listItemEnter,
    Duration baseDelay = const Duration(milliseconds: 50),
    AnimationType type = AnimationType.slideInUp,
  }) {
    final delay = Duration(milliseconds: baseDelay.inMilliseconds * index);
    
    switch (type) {
      case AnimationType.slideInUp:
        return EntranceAnimations.slideInUp(
          child: child,
          duration: duration,
          delay: delay,
        );
      case AnimationType.slideInLeft:
        return EntranceAnimations.slideInLeft(
          child: child,
          duration: duration,
          delay: delay,
        );
      case AnimationType.fadeIn:
        return EntranceAnimations.fadeIn(
          child: child,
          duration: duration,
          delay: delay,
        );
      case AnimationType.zoomIn:
        return EntranceAnimations.zoomIn(
          child: child,
          duration: duration,
          delay: delay,
        );
    }
  }
}

/// 动画类型枚举
enum AnimationType {
  slideInUp,
  slideInLeft,
  fadeIn,
  zoomIn,
}